home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_05 / test_obj / testgen.cpp < prev    next >
C/C++ Source or Header  |  1993-01-20  |  3KB  |  104 lines

  1. /* File:     Testgen.cpp
  2.    Copyright Norman Wilde 1993
  3. */
  4. #include "testgen.h"
  5. #include <iostream.h>
  6. #include <assert.h>
  7. Test_gen::Test_gen(int num_p, int max_v[]){
  8.    num_params = num_p;
  9.    max_vals = new int[num_params];
  10.    for (int i = 0; i < num_params; i++)
  11.      max_vals[i] = max_v[i];
  12. }
  13. Test_gen::~Test_gen() {
  14. //   cout << "DEBUG - Entering Test_gen::~Test_gen\n" ;
  15.    delete max_vals;
  16. }
  17. MORETESTS Test_gen::next_vector(int test_vals[]) {
  18.    cout << "Test_gen::next_vector : should be implemented by subclass\n";
  19.    return NO;
  20. }
  21. ostream & operator<< (ostream& s, Test_gen& t) {
  22.   // output num_params and max_vals as text to the stream
  23.   s << "num_params = " << t.num_params << " max_vals = {";
  24.   for (int i=0; i<t.num_params; i++)
  25.      s << t.max_vals[i] << " ";
  26.   s << "}";
  27.   return s;
  28. }
  29. int Test_gen::ok(void) {
  30.    if (num_params <1) return 0;
  31.    for (int i=0; i<num_params; i++){
  32.       if ((max_vals[i] <1) || (max_vals[i] > 19)) return 0;
  33.    }
  34. return 1;
  35. }
  36. Test_gen_varying::Test_gen_varying(int num_p, int max_v[]) :
  37.     Test_gen( num_p, max_v) {
  38.    param_to_vary = -1;
  39.    last_val = 0;
  40.    if (!ok())
  41.       cout << "Error at " << __FILE__ << " l:" << __LINE__ << "\n" ;
  42.  
  43. }
  44. MORETESTS Test_gen_varying::next_vector(int test_vals[]) {
  45.    if(!ok()) return NO;
  46.    for(int i= 0; i <num_params; i++) test_vals[i] = 0;
  47.    if(param_to_vary == -1) { // first call - return 0,0,...
  48.       param_to_vary = 0;
  49.       last_val = 0;
  50.       return YES;
  51.    }
  52.    assert( param_to_vary < num_params);
  53.    assert( last_val < max_vals[param_to_vary] );
  54.    // assert( all params <param_to_vary have been given all their vals)
  55.    // assert( last value of param_to_vary was last_val )
  56.    if(++last_val == max_vals[param_to_vary]) {
  57.       // have reached maximum - find next param to vary
  58.       last_val = 1; 
  59.       while (++param_to_vary < num_params) {
  60.      if( max_vals[param_to_vary] > 1) break;
  61.       }
  62.    }
  63.    if(param_to_vary < num_params) { // set value of param being varied
  64.       test_vals[param_to_vary] = last_val;
  65.       return YES;
  66.    }
  67.    else return NO;
  68. }
  69. Test_gen_varying::ok(void) {
  70.    if( !(Test_gen::ok()) ) return 0;
  71.    if( param_to_vary >= num_params ) return 0;
  72.    return 1;
  73. }
  74. Test_gen_combining::Test_gen_combining(int num_p, int max_v[]) :
  75.     Test_gen( num_p, max_v) {
  76.    last_val = new int[num_p];
  77.    last_val[0] = -1;
  78.    for (int i=1; i< num_p; i++) last_val[i] = 0;
  79.    if (!ok())
  80.       cout << "Error at " << __FILE__ << " l:" << __LINE__ << "\n" ;
  81.  
  82. }
  83. Test_gen_combining::~Test_gen_combining() {
  84.    delete last_val;
  85. }
  86. MORETESTS Test_gen_combining::next_vector(int test_vals[]) {
  87.    if( !ok() ) return NO;
  88.    for( int p= 0; p< num_params; p++) {
  89.       if(++(last_val[p]) < max_vals[p]) {
  90.      for(int j=0; j< p; j++) last_val[j] = 0;
  91.      for(int k=0; k<num_params; k++) test_vals[k] = last_val[k];
  92.      return YES;
  93.       }
  94.    }
  95.    return NO;
  96. }
  97. int Test_gen_combining::ok(void) {
  98.    if( !(Test_gen::ok()) ) return 0;
  99.    for(int i=0; i<num_params; i++) {
  100.      if ( last_val[i] >= max_vals[i]) return 0;
  101.    }
  102.    return 1;
  103. }
  104.